// Copyright (c) 2023 Institute of Software, Chinese Academy of Sciences
// Ripple is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

package ripple.server.tcp.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ripple.common.tcp.Handler;
import ripple.common.tcp.Message;
import ripple.common.tcp.message.MQSubscribeRequest;
import ripple.common.tcp.message.MQSubscribeResponse;
import ripple.common.tcp.message.SubscribeRequest;
import ripple.common.tcp.message.SubscribeResponse;
import ripple.server.core.Node;

import java.net.InetSocketAddress;


public class MQSubscribeRequestHandler implements Handler {
    private static final Logger LOGGER = LoggerFactory.getLogger(MQSubscribeRequestHandler.class);
    private Node node;

    private Node getNode() {
        return node;
    }

    private void setNode(Node node) {
        this.node = node;
    }

    public MQSubscribeRequestHandler(Node node) {
        this.setNode(node);
    }

    @Override
    public Message handle(ChannelHandlerContext channelHandlerContext, Message message) {
        MQSubscribeRequest mqSubscribeRequest = (MQSubscribeRequest) message;
//        InetSocketAddress localAddress = ((NioSocketChannel) channelHandlerContext.channel()).localAddress();
//        InetSocketAddress remoteAddress = ((NioSocketChannel) channelHandlerContext.channel()).remoteAddress();
//
//        LOGGER.info("[MQSubscribeRequestHandler] [{}:{}<-->{}:{}] Receive SUBSCRIBE request. UUID = {}" +
//                        ", topic = {}, callback address = {}, callback port = {}."
//                , localAddress.getHostString(), localAddress.getPort(), remoteAddress.getHostString()
//                , remoteAddress.getPort(), mqSubscribeRequest.getUuid()
//                , mqSubscribeRequest.getTopic(), mqSubscribeRequest.getCallbackAddress(), mqSubscribeRequest.getCallbackPort());

        this.getNode().subscribe(mqSubscribeRequest.getCallbackAddress(), mqSubscribeRequest.getCallbackPort()
                , mqSubscribeRequest.getTopic());

        MQSubscribeResponse mqSubscribeResponse = new MQSubscribeResponse();
        mqSubscribeResponse.setUuid(mqSubscribeRequest.getUuid());
        mqSubscribeResponse.setSuccess(true);
//
//        LOGGER.info("[MQSubscribeRequestHandler] [{}:{}<-->{}:{}] Send SUBSCRIBE response. Success = {}."
//                , localAddress.getHostString(), localAddress.getPort(), remoteAddress.getHostString()
//                , remoteAddress.getPort(), mqSubscribeResponse.isSuccess());
        return mqSubscribeResponse;
    }
}